unit uPreCondition;

interface

uses
  uRefValue;

type

  TBevNodePrecondition = class
    public
      function ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;abstract;
  end;

  TBevNodePreconditionTRUE = class(TBevNodePrecondition)
    public
      function ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;
  end;

  TBevNodePreconditionFALSE = class(TBevNodePrecondition)
    public
      function ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;
  end;

  TBevNodePreconditionNOT = class(TBevNodePrecondition)
    private
      m_lhs: TBevNodePrecondition;

    public
      function  ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;
      constructor Create(lhs: TBevNodePrecondition);
      destructor Destroy; override;
  end;

  TBevNodePreconditionAND = class(TBevNodePrecondition)
    private
      m_lhs, m_rhs: TBevNodePrecondition;

    public
      function ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;

      constructor Create(lhs, rhs: TBevNodePrecondition);
      destructor Destroy; override;
  end;

  TBevNodePreconditionOR = class(TBevNodePrecondition)
    private
      m_lhs, m_rhs: TBevNodePrecondition;

    public
     function ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;

     constructor Create(lhs, rhs: TBevNodePrecondition);
     destructor Destroy; override;
  end;

  TBevNodePreconditionXOR = class(TBevNodePrecondition)
    private
      m_lhs, m_rhs: TBevNodePrecondition;

    public
     function ExternalCondition(const input: TBevNodeInputParam): Boolean; virtual;

     constructor Create(lhs, rhs: TBevNodePrecondition);
     destructor Destroy; override;
  end;

implementation

{ TBevNodePreconditionTRUE }

function TBevNodePreconditionTRUE.ExternalCondition(
  const input: TBevNodeInputParam): Boolean;
begin
  result := True;
end;

{ TBevNodePreconditionFALSE }

function TBevNodePreconditionFALSE.ExternalCondition(
  const input: TBevNodeInputParam): Boolean;
begin
  result := False;
end;

{ TBevNodePreconditionNOT }

constructor TBevNodePreconditionNOT.Create(lhs: TBevNodePrecondition);
begin
  assert(Assigned(lhs));
  m_lhs := lhs;
end;

destructor TBevNodePreconditionNOT.Destroy;
begin
  FreeAndNil(m_lhs);
  inherited;
end;

function TBevNodePreconditionNOT.ExternalCondition(
  const input: TBevNodeInputParam): Boolean;
begin
  result := not m_lhs.ExternalCondition(input);
end;

{ TBevNodePreconditionAND }

constructor TBevNodePreconditionAND.Create(lhs, rhs: TBevNodePrecondition);
begin
  assert(Assigned(lhs) and Assigned(rhs));
  m_lhs := lhs;
  m_rhs := rhs;
end;

destructor TBevNodePreconditionAND.Destroy;
begin
  freeandnil(m_lhs);
  freeandnil(m_rhs);
  inherited;
end;

function TBevNodePreconditionAND.ExternalCondition(
  const input: TBevNodeInputParam): Boolean;
begin
  result := m_lhs.ExternalCondition(input) and m_rhs.ExternalCondition(input);
end;

{ TBevNodePreconditionOR }

constructor TBevNodePreconditionOR.Create(lhs, rhs: TBevNodePrecondition);
begin
  assert(Assigned(lhs) and Assigned(rhs));
  m_lhs := lhs;
  m_rhs := rhs;
end;

destructor TBevNodePreconditionOR.Destroy;
begin
  freeandnil(m_lhs);
  freeandnil(m_rhs);
  inherited;
end;

function TBevNodePreconditionOR.ExternalCondition(
  const input: TBevNodeInputParam): Boolean;
begin
  result := m_lhs.ExternalCondition(input) or m_rhs.ExternalCondition(input);
end;

{ TBevNodePreconditionXOR }

constructor TBevNodePreconditionXOR.Create(lhs, rhs: TBevNodePrecondition);
begin
  assert(Assigned(lhs) and Assigned(rhs));
  m_lhs := lhs;
  m_rhs := rhs;
end;

destructor TBevNodePreconditionXOR.Destroy;
begin
  freeandnil(m_lhs);
  freeandnil(m_rhs);
  inherited;
end;

function TBevNodePreconditionXOR.ExternalCondition(
  const input: TBevNodeInputParam): Boolean;
begin
  result := m_lhs.ExternalCondition(input) xor m_rhs.ExternalCondition(input);
end;

end.
